﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    class InputHandler
    {
        public event EventHandler Left;
        public event EventHandler Right;
        public event EventHandler Down;
        public event EventHandler Rotation;

        public void QueryKeyBoard()
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo kInfo = Console.ReadKey(true);

                switch (kInfo.Key)
                {
                    case ConsoleKey.LeftArrow:
                    case ConsoleKey.A:
                        OnLeft();
                        break;
                    case ConsoleKey.RightArrow:
                    case ConsoleKey.D:
                        OnRight();
                        break;
                    case ConsoleKey.DownArrow:
                    case ConsoleKey.S:
                        OnDown();
                        break;
                    case ConsoleKey.Spacebar:
                    case ConsoleKey.Enter:
                        OnRotate();
                        break;
                }
            }
        }
        protected virtual void OnLeft()
        {
            if (this.Left != null)
                this.Left(this, EventArgs.Empty);
        }
        protected virtual void OnRight()
        {
            if (this.Right != null)
                this.Right(this, EventArgs.Empty);
        }
        protected virtual void OnDown()
        {
            if (this.Down != null)
                this.Down(this, EventArgs.Empty);
        }
        protected virtual void OnRotate()
        {
            if (this.Rotation != null)
                this.Rotation(this, EventArgs.Empty);
        }
    }
}
